package model;

import java.util.ArrayList;

import controller.RuntimeState;

public class History {

	private ArrayList<RuntimeState> history = new ArrayList<RuntimeState>();

	private int historyPointer = 0;

	public History() {
		RuntimeState runtimeState = RuntimeState.buildCurrentRuntimeState();
		history.add(runtimeState);
	}

	public RuntimeState back() {
		if (historyPointer > 0) {
			historyPointer--;
			return history.get(historyPointer);
		}
		return history.get(0);
	}

	public RuntimeState next() {
		if (hasNext()) {
			historyPointer++;
			return history.get(historyPointer);
		}
		return getCurrent();
	}

	public RuntimeState add(RuntimeState next) {
		if (!hasNext()) {
			historyPointer++;
			history.add(next);
		}
		return getCurrent();
	}

	public boolean hasNext() {
		return historyPointer < history.size() - 1;
	}

	public RuntimeState getCurrent() {
		return history.get(historyPointer);
	}

	public void reset() {
		historyPointer = 0;
		history = new ArrayList<RuntimeState>();
		RuntimeState runtimeState = RuntimeState.buildCurrentRuntimeState();
		runtimeState.setAccumulator(0);
		runtimeState.setPointer(0);
		history.add(runtimeState);
	}
}
